package dao

import (
	"fmt"
	"lib/structs/tables"
	"lib/structs/vap"
	"vap/global"
)

var ShHostDao = new(shHostDao)

type shHostDao struct{}

func (d shHostDao) Create(m *tables.VapShHost) (err error) {
	err = global.DB.Create(m).Error
	return
}
func (d shHostDao) FindById(id int64) (m tables.VapShHost, err error) {
	err = global.DB.Where("id = ?", id).First(&m).Error
	return
}

func (d shHostDao) FindList(in vap.SearchShHost) (li []tables.VapShHost, total int64, err error) {
	err = global.DB.Model(&tables.VapShHost{}).
		Where("ip like ?", fmt.Sprintf("%%%s%%", in.Ip)).
		Count(&total).Limit(in.PageSize).Offset((in.PageNum - 1) * in.PageSize).Find(&li).Error
	return
}

func (d shHostDao) DeleteById(id int64) (err error) {
	err = global.DB.Where("id = ?", id).Delete(&tables.VapShHost{}).Error
	return
}
